<template>
  <div class="login-box">
    <div class="login-content">
      <div class="login-form">
        <el-form
          ref="ruleForm"
          class="lg-form"
          :rules="rules"
          :model="ruleForm"
          :label-width="$i18n.locale=='zh_cn'? '70px':'120px'"
          status-icon
          label-position="left"
        >
          <el-form-item prop="username" :label="$t('SYS.userName')" class="formItem">
            <el-input
              type="text"
              v-model="ruleForm.username"
              autocomplete="off"
              :clearable="true"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password" :label="$t('SYS.passWord')" class="formItem">
            <el-input
              show-password
              type="password"
              v-model="ruleForm.password"
              :clearable="true"
              autocomplete="new-password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :disabled="isLogin" :loading="isLogin" @click="submitForm('ruleForm')">
              {{$t('SYS.logIn')}}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
export default {
  data() {
    return {
      isLogin: false,
      rules: {
        password: [{ required: true, message: this.$t('SYS.inputUserName'), trigger: "blur" }],
        username: [{ required: true, message: this.$t("SYS.inputPassword"), trigger: "blur" }]
      },
      ruleForm: {
        username: "",
        password: ""
      }
    };
  },
  mounted() {
    var that = this;
    document.onkeydown = function() {
      var key = window.event.keyCode;
      if (key == 13) {
        that.submitForm("ruleForm");
      }
    };
  },
  beforeDestroy() {
    document.onkeydown = undefined;
  },
  methods: {
    // 获取用户登录信息
    getUserLoginInfo() {
      var p = new Promise((resolve, reject) => {
        this.$http
          .get(this.$baseUrl.sysUserInfo)
          .then(res => {
            if (res.data.code !== 0) {
              return this.$message.error(res.msg);
            }
            resolve(res);
          })
          .catch(error => {
            reject(error);
          });
      });
      return p;
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.isLogin = true;
          // if(this.ruleForm.username == "admin" && this.ruleForm.password == "admin"){
          //   this.$store.commit('setUserInfo',{username:"admin",realName:'管理员'})
          //   this.$router.replace({ path: "/home" });
          // }
          // return
          this.$http
            .post(this.$baseUrl.login, this.ruleForm)
            .then(res => {
              this.isLogin = false;
              if (res.data.code != 0) {
                return this.$message.error(res.data.msg);
              }
              Cookies.set('token', res.data.data.token)
              this.getUserLoginInfo()
                .then(r => {
                  this.$store.commit('setUserInfo',r.data.data)
                  this.$router.replace({ path: "/home" });
                })
                .catch(err => {
                  console.error(err);
                });
            })
            .catch(err => {
              this.isLogin = false;
            });
        }
      });
    }
  }
};
</script>

<style scoped>
.login-box {
  height: 100vh;
  position: relative;
  background-color: #000000;
}
.login-box .loginImg {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  user-select: none;
}
.login-box .login-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 1px solid #333;
  border-radius: 5px;
  background-color: rgb(255, 255, 255);
}
.login-form {
  padding: 84px 97px;
}
.lg-form >>> .el-form-item .el-form-item__label {
  color: #333;
}
</style>
